/*
 * Licensed to csti consulting 
 * You may obtain a copy of the License at
 *
 * http://www.csticonsulting.com
 * Copyright (c) 2006-Aug 24, 2010 Consultation CS-TI inc. 
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package com.salesmanager.core.utils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

import com.alibaba.druid.sql.visitor.functions.Char;
import com.salesmanager.core.constants.Constants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


public class DateUtil {

    private static final Logger LOGGER = LoggerFactory.getLogger(DateUtil.class);

    private Date startDate = new Date(new Date().getTime());
    private Date endDate = new Date(new Date().getTime());
    private final static String LONGDATE_FORMAT = "yyyy-MM-dd HH:mm:ss";
    private final static String LONGDATE_FORMAT_MINUTE = "yyyy-MM-dd HH:mm";

    /**
     *
     */

    public static String getMonth() {
        Calendar calender = Calendar.getInstance();
        calender.setTime(new Date());
        Integer i = calender.get(Calendar.MONTH)+1;
        return i.toString();
    }

    /**
     * yyyyMMddHHMM
     *
     * @param
     * @return
     */

    public static String format2StringDate(String date) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmm");
        Date date1 = sdf.parse(date);
        String formatDate = new SimpleDateFormat("yyyy-MM-dd").format(date1);
        return formatDate;
    }

    public static Long FiveMinuteStart() throws ParseException {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmm");
        String fiveMinuteEnd = DateUtil.FiveMinuteEnd() + "";
        Date date = formatter.parse(fiveMinuteEnd);
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(date);
        calendar.add(calendar.MINUTE, -5);
        date = calendar.getTime();
        String timeStart = formatter.format(date);
        return Long.parseLong(timeStart);
    }

    public static Long FiveMinuteEnd() {
        Date date = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmm");
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(date);
        String timeEnd = formatter.format(date);
        String str = timeEnd.substring(timeEnd.length() - 1, timeEnd.length());
        if (Integer.parseInt(str) <= 4) {
            timeEnd = timeEnd.substring(0, 11) + "0";
        } else {
            timeEnd = timeEnd.substring(0, 11) + "5";
        }
        return Long.parseLong(timeEnd);
    }

    public static String TodaystartTime() {
        SimpleDateFormat Sformat = new SimpleDateFormat("yyyy-MM-dd 00:00:00");
        String TodaystartTime = Sformat.format(new Date());
        return TodaystartTime;
    }

    public static String TodayEndTime() {
        SimpleDateFormat Eformat = new SimpleDateFormat("yyyy-MM-dd 23:59:59");
        String TodayendTime = Eformat.format(new Date());
        return TodayendTime;
    }

    public static Long minuteStart() {
        SimpleDateFormat Sformat = new SimpleDateFormat("yyyyMMdd0000");
        String TodaystartTime = Sformat.format(new Date());
        return Long.parseLong(TodaystartTime);
    }

    public static Long minuteEnd() {
        Date date = new Date();
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(date);
        calendar.add(calendar.DATE, 1);
        date = calendar.getTime(); //这个时间就是日期往后推一天的结果
        SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd0000");
        String dateString = formatter.format(date);
        return Long.parseLong(dateString);
    }

    /**
     * Generates a time stamp
     * yyyymmddhhmmss
     *
     * @return
     */
    public static String generateTimeStamp() {
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmSS");
        return format.format(new Date());
    }


    /**
     * @param dateStr   2015-10-22 13:23:00
     * @param sliceTime M/H/D/M/Y
     * @return M:20151022132300 H:2015102213
     */
    public static Long formatNumberDate(String dateStr, char sliceTime) {
        if (dateStr == null) return null;
        String dateStr1 = null;
        try {
            switch (sliceTime) {
                case 'M':
                    dateStr1 = dateStr.replaceAll("-", "").replaceAll(" ", "").replaceAll(":", "");
                    if (dateStr1.substring(10).equals("5959")) {
                        SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmm");
                        Date date = formatter.parse(dateStr1.substring(0, 12));
                        Calendar calendar = new GregorianCalendar();
                        calendar.setTime(date);
                        calendar.add(Calendar.MINUTE, 1);
                        date = calendar.getTime();
                        dateStr = formatter.format(date);
                    } else {
                        dateStr = dateStr1.substring(0, 12);
                    }
            }
        } catch (Exception e) {
            LOGGER.error("DateUtil.formatNumberDate ERROR! dateStr:" + dateStr, e);
        }
        if (dateStr1 != null) {
            return Long.parseLong(dateStr);
        }

        return null;
    }

    /**
     * yyyy-MM-dd
     *
     * @param dt
     * @return
     */
    public static String formatDate(Date dt) {

        if (dt == null)
            return null;
        SimpleDateFormat format = new SimpleDateFormat(Constants.DEFAULT_DATE_FORMAT);
        return format.format(dt);

    }

    public static String formatYear(Date dt) {

        if (dt == null)
            return null;
        SimpleDateFormat format = new SimpleDateFormat(Constants.DEFAULT_DATE_FORMAT_YEAR);
        return format.format(dt);

    }

    public static String formatLongDate(Date date) {

        if (date == null)
            return null;
        SimpleDateFormat format = new SimpleDateFormat(LONGDATE_FORMAT);
        return format.format(date);
    }

    public static String formatLongDateMinute(Date date) {

        if (date == null)
            return null;
        SimpleDateFormat format = new SimpleDateFormat(LONGDATE_FORMAT_MINUTE);
        return format.format(date);
    }

    /**
     * yy-MMM-dd
     *
     * @param dt
     * @return
     */
    public static String formatDateMonthString(Date dt) {

        if (dt == null)
            return null;
        SimpleDateFormat format = new SimpleDateFormat(Constants.DEFAULT_DATE_FORMAT);
        return format.format(dt);
    }

    public static Date getDate(String date) throws Exception {
        DateFormat myDateFormat = new SimpleDateFormat(Constants.DEFAULT_DATE_FORMAT);
        return myDateFormat.parse(date);
    }

    public static Date formatString2Date(String date) throws Exception {
        DateFormat myDateFormat = new SimpleDateFormat(Constants.DEFAULT_STRING_FORMAT);
        return myDateFormat.parse(date);
    }


    public static Date getMinuteDate(String date) throws Exception {
        DateFormat myDateFormat = new SimpleDateFormat(Constants.DEFAULT_DATE_FORMAT_MINUTE);
        return myDateFormat.parse(date);
    }

    public static Date addDaysToCurrentDate(int days) {
        Calendar c = Calendar.getInstance();
        c.setTime(new Date());
        c.add(Calendar.DATE, days);
        return c.getTime();

    }

    public static Date getDate() {

        return new Date(new Date().getTime());

    }

    public static String getPresentDate() {

        Date dt = new Date();

        SimpleDateFormat format = new SimpleDateFormat(Constants.DEFAULT_DATE_FORMAT);
        return format.format(new Date(dt.getTime()));
    }

    public static String getPresentYear() {

        Date dt = new Date();

        SimpleDateFormat format = new SimpleDateFormat("yyyy");
        return format.format(new Date(dt.getTime()));
    }

    public Date getEndDate() {
        return endDate;
    }

    public Date getStartDate() {
        return startDate;
    }
}
